package com.example.mooctao.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mooctao.R;

public class NewsDetailFragment extends Fragment {
    private ImageView newsImage;
    private TextView newsTitle, newsContent;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_detail, container, false);

        newsImage = view.findViewById(R.id.news_image);
        newsTitle = view.findViewById(R.id.news_title);
        newsContent = view.findViewById(R.id.news_content);
        ImageView btnBack = view.findViewById(R.id.btn_back);

        btnBack.setOnClickListener(v -> getParentFragmentManager().popBackStack());

        // 获取传递的数据
        Bundle bundle = getArguments();
        if (bundle != null) {
            int imgResId = bundle.getInt("image", -1); // -1 作为默认值，避免崩溃
            String title = bundle.getString("title", "默认标题");
            String content = bundle.getString("content", "暂无内容");

            if (imgResId != -1) {
                newsImage.setImageResource(imgResId);
            } else {
                Toast.makeText(getContext(), "图片资源加载失败", Toast.LENGTH_SHORT).show();
            }

            newsTitle.setText(title);
            newsContent.setText(content);
        } else {
            Toast.makeText(getContext(), "数据加载失败", Toast.LENGTH_SHORT).show();
        }

        return view;
    }
}
